package com.kitkat.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.kitkat.annotation.UserTokenCheck;
import com.kitkat.dao.AbstractTokenDAO;
import com.kitkat.dao.ProjectDAO;
import com.kitkat.dao.UserDAO;
import com.kitkat.entities.MyIcon;
import com.kitkat.entities.Project;
import com.kitkat.entities.User;
import com.kitkat.entities.UserToken;
import com.kitkat.service.ProjectService;
import com.kitkat.util.CodeAndMessage;

@Component
public class ProjectServiceImpl implements ProjectService{
	@Autowired
	private ProjectDAO projectDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private AbstractTokenDAO<String, UserToken> userTokenDAO;
	
	@Override
	@Transactional
	@UserTokenCheck(tokenIndex=0)
	public Map<String,Object> newProject(String token,String name, String description, MyIcon icon,String templateId) {
		Map<String,Object> hm = new HashMap<String,Object>();
		UserToken userToken = this.userTokenDAO.get(token);
		User user = userToken.getUser();
		Project project = new Project(name, description,new Date(), new Date(), user, icon);
		this.projectDAO.saveProject(project);
		hm.put("code", CodeAndMessage.SuccessCode);
		hm.put("message", CodeAndMessage.SuccessMessage);
		hm.put("result",null);
		return hm;
	}

	@Override
	@Transactional
	@UserTokenCheck(tokenIndex=0)
	public Map<String,Object> getProjectByToken(String token) {
		Map<String,Object> hm = new HashMap<String,Object>();
		UserToken userToken = this.userTokenDAO.get(token);
		User user = userToken.getUser();
		List<Project> list = this.projectDAO.getProjectByUserId(user.getId());
		hm.put("code", CodeAndMessage.SuccessCode);
		hm.put("message", CodeAndMessage.SuccessMessage);
		hm.put("result", list);
		return hm;
	}

	@Override
	@Transactional
	@UserTokenCheck(tokenIndex=0)
	public Map<String, Object> getProjectByProjectId(String token,String projectId) {
		Map<String,Object> hm = new HashMap<String,Object>();
		UserToken userToken = this.userTokenDAO.get(token);
		Project project = this.projectDAO.getProjectById(projectId);
		if(project!=null){
			User user = userToken.getUser();
			if(!project.getUser().getId().equals(user.getId())){
				hm.put("code", CodeAndMessage.SuccessCode);
				hm.put("message", "该工程不属于该用户！");
				hm.put("result", null);
				return hm;
			}
		}
		hm.put("code", CodeAndMessage.SuccessCode);
		hm.put("message", CodeAndMessage.SuccessMessage);
		hm.put("result", project);
		return hm;
	}

	@Override
	@Transactional
	@UserTokenCheck
	public Map<String, Object> updateProjectByProjectId(String token,
			String name, String description, String content, String projectId) {
		Map<String,Object> hm = new HashMap<String,Object>();
		UserToken userToken = this.userTokenDAO.get(token);
		Project project = this.projectDAO.getProjectById(projectId);
		if(project!=null){
			User user = userToken.getUser();
			if(!project.getUser().getId().equals(user.getId())){
				hm.put("code", CodeAndMessage.SuccessCode);
				hm.put("message", "该工程不属于该用户！");
				hm.put("result", null);
				return hm;
			}
			else{
				if(StringUtils.hasText(name)){
					project.setName(name);
				}
				if(StringUtils.hasText(description)){
					project.setDescription(description);
				}
				if(StringUtils.hasText(content)){
					project.setContent(content);
				}
				project.setLastModifyDate(new Date());
				hm.put("code", CodeAndMessage.SuccessCode);
				hm.put("message", CodeAndMessage.SuccessMessage);
				hm.put("result", null);
				return hm;
			}
		}
		hm.put("code", CodeAndMessage.SuccessCode);
		hm.put("message", "该工程不存在！");
		hm.put("result", null);
		return hm;
	}

	@Override
	@Transactional
	@UserTokenCheck
	public Map<String, Object> deleteProjectByProjectId(String token,
			String projectId) {
		Map<String,Object> hm = new HashMap<String,Object>();
		UserToken userToken = this.userTokenDAO.get(token);
		Project project = this.projectDAO.getProjectById(projectId);
		if(project!=null){
			User user = userToken.getUser();
			if(!project.getUser().getId().equals(user.getId())){
				hm.put("code", CodeAndMessage.SuccessCode);
				hm.put("message", "该工程不属于该用户！");
				hm.put("result", null);
				return hm;
			}
			else{
				this.projectDAO.deleteProject(project);
				hm.put("code", CodeAndMessage.SuccessCode);
				hm.put("message", CodeAndMessage.SuccessMessage);
				hm.put("result", null);
				return hm;
			}
		}
		hm.put("code", CodeAndMessage.SuccessCode);
		hm.put("message", "该工程不存在！");
		hm.put("result", null);
		return hm;
	}
	
	
}
